package com.qo.qsdk.ui.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qo.qsdk.model.HisModel;
import com.qo.qsdk.utils.DynamicUtils;

import java.util.List;

public class HisAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public interface HisAdapterLister{
        void onItemClick(int pos);
    }

    private Context mContext;
    private List<HisModel> mDatas;
    HisAdapterLister mHisAdapterLister;

    public HisAdapter(Context context, List<HisModel> datas) {
        mContext = context;
        mDatas = datas;
    }

    public void setHisAdapterLister(HisAdapterLister accountListAdapterLister) {
        this.mHisAdapterLister = accountListAdapterLister;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (viewType == HisModel.ITEM_TYPE.ITEM_TYPE_ITEM.ordinal()) {
            return new NormalHolder(inflater.inflate(DynamicUtils.layout(parent.getContext(), "qsdk_his_item"), parent, false));
        }
        return new SectionHolder(inflater.inflate(DynamicUtils.layout(parent.getContext(), "qsdk_his_item_title"), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SectionHolder) {
            final SectionHolder sectionHolder = (SectionHolder) holder;
           sectionHolder.mTV.setText(mDatas.get(position).getAccount());
        }else{
            NormalHolder normalHolder = (NormalHolder) holder;
            normalHolder.mServer.setText(mDatas.get(position).getServer());
            normalHolder.mLevel.setText(mDatas.get(position).getLevel());
            normalHolder.mRole.setText(mDatas.get(position).getRole());
        }
    }

    @Override
    public int getItemViewType(int position) {
        return mDatas.get(position).getItemType().ordinal();
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class NormalHolder extends RecyclerView.ViewHolder {

        public TextView mServer;
        public TextView mLevel;
        public TextView mRole;

        public NormalHolder(View itemView) {
            super(itemView);
            mServer = itemView.findViewById(DynamicUtils.id(itemView.getContext(), "server"));
            mLevel = itemView.findViewById(DynamicUtils.id(itemView.getContext(), "level"));
            mRole = itemView.findViewById(DynamicUtils.id(itemView.getContext(), "role"));
        }
    }

    public class SectionHolder extends RecyclerView.ViewHolder {

        public TextView mTV;
        public TextView mBtn;

        public SectionHolder(View itemView) {
            super(itemView);
            mTV = itemView.findViewById(DynamicUtils.id(itemView.getContext(), "t"));
            mBtn = itemView.findViewById(DynamicUtils.id(itemView.getContext(), "btn"));
            mBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mHisAdapterLister != null) mHisAdapterLister.onItemClick(getAdapterPosition());
                }
            });
        }
    }
}
